
PLUG.Platform = PLUG_PLATFORM_SERVER

PLUG.ClassName = "sh_antiminge"
PLUG.PrintName = "Anti-Minge Module"
PLUG.Author = "thomasfn"

function PLUG:AddHooks()
	plugs.Hook( PLUG_PLATFORM_SERVER, "PlayerInitialSpawn" )
	plugs.Hook( PLUG_PLATFORM_SERVER, "Tick" )
	plugs.Hook( PLUG_PLATFORM_SERVER, "PlayerSay" )
end

function PLUG:PlayerInitialSpawn( ply )
	ply.JoinName = ply:Name()
end

function PLUG:Tick()
	local ply
	for _, ply in pairs( player.GetAll() ) do
		ply.NameWarnings = ply.NameWarnings or 0
		if (ply:Name() != ply.JoinName) then
			ply.NameWarnings = ply.NameWarnings + 1
		end
		if (ply.NameWarnings >= 3) then
			ply:Log( "antiminge", "Was kicked due to name change" )
			plugs.Call( "SendChatNotice", ply.JoinName .. " was kicked becuase his/her name changed!" )
			ply.NameWarnings = 0
			ply:Kick( "Name changed" )
		end
		/*ply.HaxWarnings = ply.HaxWarnings or 0
		local hacking = plugs.Call( "CheckForSpeedHack", ply )
		if (hacking) then ply.HaxWarnings = ply.HaxWarnings + 1 end
		if (ply.HaxWarnings >= 10) then
			ply:Log( "antiminge", "Was kicked due to speed hacking" )
			plugs.Call( "SendChatNotice", ply.JoinName .. " was kicked because he/she speed hacked!" )
			ply.HaxWarnings = 0
			ply:Kick( "Speed hacking" )
		end*/
	end
end

local trace = {}
function PLUG:CheckForSpeedHack( ply )
	if (!ply:OnGround()) then return end
	if (ply:InVehicle()) then return end
	if (ply:GetMoveType() != MOVETYPE_WALK) then return end
	local vel = ply:GetVelocity():Length()
	if (vel == 0) then return end
	local highest = ply:GetMaxSpeed() * 1.25
	if (vel > highest) then
		trace.start = ply:GetPos() + Vector( 0, 0, 32 )
		trace.endpos = ply:GetPos() - Vector( 0, 0, 32 )
		trace.filter = ply
		local tr = util.TraceLine( trace )
		return tr.HitWorld
	end
end

function PLUG:PlayerSay( ply, text, public )
	ply.ChatWarnings = ply.ChatWarnings or 0
	ply.LastSay = ply.LastSay or ""
	if (string.lower( text ) == ply.LastSay) then
		ply.ChatWarnings = ply.ChatWarnings + 1
	end
	ply.LastSay = string.lower( text )
	if (string.upper( text ) == text) && (string.len( text ) >= 3) then
		ply.ChatWarnings = ply.ChatWarnings + 1
	end
	if (ply.ChatWarnings >= 5) then
		ply:Log( "antiminge", "Was kicked due to chat misuse" )
		plugs.Call( "SendChatNotice", ply.JoinName .. " was kicked becuase he misused the chat system!" )
		ply.ChatWarnings = 0
		ply:Kick( "Chat misuse" )
	end
end